package com.wdy.service.impl;

import java.util.LinkedList;
import java.util.List;
import com.wdy.dao.TenementDao;
import com.wdy.po.Building;
import com.wdy.po.Tenement;
import com.wdy.service.TenementManager;
import com.wdy.util.PageUtil;
import com.wdy.vo.Condition;
import com.wdy.vo.Page;
import com.wdy.vo.Result;
@SuppressWarnings("unchecked")
public class TenementManagerImpl implements TenementManager {

	private TenementDao tenementDao;

	public void setTenementDao(TenementDao tenementDao) {
		this.tenementDao = tenementDao;
	}

	public void saveTenement(Tenement tenement) {
		tenementDao.save(tenement);
	}

	public void removeTenement(Tenement tenement) {
		tenementDao.delete(tenement);
	}

	public void updateTenement(Tenement tenement) {
		tenementDao.attachDirty(tenement);
	}

	public Tenement getTenement(Integer id) {
		return tenementDao.findById(id);
	}

	public List<Tenement> listAll() {
		return tenementDao.findAll();
	}

	public List<Tenement> findByExample(Tenement tenement) {
		return tenementDao.findByExample(tenement);
	}

	public List<Tenement> findByID(String value) {
		return tenementDao.findByID(value);
	}

	public List<Building> findBuilding(Object value) {
		return tenementDao.findBuilding(value);
	}

	@Override
	public Result query(List<Condition> conList, Page page) {
		// TODO Auto-generated method stub
		int totalRecords = tenementDao.getQueryCount(conList);
		page = PageUtil.createPage(page, totalRecords);
		List tempList = (List) tenementDao.query(conList, page);
		List<Tenement> tenements = new LinkedList<Tenement>();
		for (Object o : tempList) {
			if (o instanceof Object[]) {
				Object[] oArray = (Object[]) o;
				for (Object oo : oArray) {
					if (oo instanceof Tenement) {
						tenements.add((Tenement) oo);
					}
				}
			} else {
				if (o instanceof Tenement) {
					tenements.add((Tenement) o);
				}
			}
		}
		return new Result(page, tenements);
	}

	@Override
	public void removeTenements(Object[] objects) {
		// TODO Auto-generated method stub
		tenementDao.deleteBatch(objects);
	}
	
}
